//
//  LoginInViewController.m
//  MJApp
//
//  Created by 王亮 on 15/8/25.
//  Copyright (c) 2015年 王亮. All rights reserved.
//

#import "LoginInViewController.h"
#import "FindPasswordViewController.h"

@interface LoginInViewController ()

@property(nonatomic,strong)UITextField *nameTextFiled;
@property(nonatomic,strong)UITextField *passwordTextField;

@end

@implementation LoginInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initUI];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initUI {
    
    self.view.backgroundColor = Color_Back_Black;
    UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    closeBtn.center = CGPointMake(IOS_IPHONE_WINDOW_WIDE - CGRectGetWidth(closeBtn.frame) * 0.5 - 10, 64 - CGRectGetHeight(closeBtn.frame) * 0.5);
    [closeBtn setImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
    [self.view addSubview:closeBtn];
    [closeBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, CGRectGetMinY(closeBtn.frame), 30, 30)];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [self.view addSubview:backBtn];
    [backBtn addTarget:self action:@selector(up) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(closeBtn.frame), 90, 90)];
    headImageView.center = CGPointMake(CGRectGetWidth(self.view.frame) / 2, headImageView.center.y);
    headImageView.image = [UIImage imageNamed:@"head"];
    headImageView.layer.cornerRadius = CGRectGetWidth(headImageView.frame) / 2;
    headImageView.clipsToBounds = YES;
    [self.view addSubview:headImageView];
    
    UIImage *nameImage = [UIImage imageNamed:@"会员登陆"];
    UIImageView *nameImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(headImageView.frame) + 20, nameImage.size.width, nameImage.size.height)];
    nameImageView.image = nameImage;
    nameImageView.center = CGPointMake(CGRectGetWidth(self.view.frame) / 2, nameImageView.center.y);
    [self.view addSubview:nameImageView];
    
    CGFloat wide = 0.6 * CGRectGetWidth(self.view.frame);
    CGFloat centerX = CGRectGetWidth(self.view.frame) / 2;
    UITextField *nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(nameImageView.frame) + 20, wide, 30)];
    nameTextField.placeholder = @"输入手机号";
    nameTextField.layer.cornerRadius = CGRectGetHeight(nameTextField.frame) / 2;
    nameTextField.center = CGPointMake(centerX, nameTextField.center.y);
    nameTextField.backgroundColor = [UIColor whiteColor];
    nameTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
    nameTextField.leftViewMode = UITextFieldViewModeAlways;
    nameTextField.font = [UIFont systemFontOfSize:15];
    nameTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:nameTextField];
    self.nameTextFiled = nameTextField;
    
    UITextField *passTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(nameTextField.frame) + 20, wide, 30)];
    passTextField.placeholder = @"输入密码";
    passTextField.secureTextEntry = YES;
    passTextField.layer.cornerRadius = CGRectGetHeight(passTextField.frame) / 2;
    passTextField.center = CGPointMake(centerX, passTextField.center.y);
    passTextField.backgroundColor = [UIColor whiteColor];
    passTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
    passTextField.leftViewMode = UITextFieldViewModeAlways;
    passTextField.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:passTextField];
    self.passwordTextField = passTextField;
    
    UIButton *loginIn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(passTextField.frame) + 20, wide, 30)];
    [loginIn setTitle:@"登 录" forState:UIControlStateNormal];
    loginIn.backgroundColor = RGB(228, 41, 9);
    loginIn.center = CGPointMake(centerX, loginIn.center.y);
    loginIn.layer.cornerRadius = CGRectGetHeight(loginIn.frame) / 2;
    [loginIn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginIn];
    
    
    UILabel *descLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(loginIn.frame) + 20, 100, 30)];
    descLabel.center = CGPointMake(CGRectGetWidth(self.view.frame) / 2, descLabel.center.y);
    descLabel.text = @"社交媒体登录";
    descLabel.font = [UIFont systemFontOfSize:15];
    descLabel.textColor = [UIColor whiteColor];
    descLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:descLabel];
    
    UIImage *weixinImage = [UIImage imageNamed:@"weixin"];
    UIImage *weiboImage = [UIImage imageNamed:@"weibo"];
    UIImage *QQImage = [UIImage imageNamed:@"QQ"];
    UIButton *weixinBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(descLabel.frame) + 10, weixinImage.size.width, weixinImage.size.height)];
    weixinBtn.center = CGPointMake(CGRectGetWidth(self.view.frame) / 2, weixinBtn.center.y);
    [weixinBtn setImage:weixinImage forState:UIControlStateNormal];
    [self.view addSubview:weixinBtn];
    UIButton *weiboBtn = [[UIButton alloc] initWithFrame:CGRectMake(0,0, weixinImage.size.width, weixinImage.size.height)];
    weiboBtn.center = CGPointMake(CGRectGetWidth(self.view.frame) / 2 - weixinImage.size.width - 20, weixinBtn.center.y);
    [weiboBtn setImage:weiboImage forState:UIControlStateNormal];
    [weiboBtn addTarget:self action:@selector(weiBo) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:weiboBtn];
    UIButton *QQBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, weixinImage.size.width, weixinImage.size.height)];
    QQBtn.center = CGPointMake(CGRectGetWidth(self.view.frame) / 2 + weixinImage.size.width + 20, weixinBtn.center.y);
    [QQBtn addTarget:self action:@selector(QQ) forControlEvents:UIControlEventTouchUpInside];
    [QQBtn setImage:QQImage forState:UIControlStateNormal];
    [self.view addSubview:QQBtn];
    
    UIButton *forgetBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(weixinBtn.frame) + 20, 100, 30)];
    [forgetBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
    forgetBtn.center = CGPointMake(CGRectGetWidth(self.view.frame) / 2, forgetBtn.center.y);
    [self.view addSubview:forgetBtn];
    [forgetBtn addTarget:self action:@selector(forget) forControlEvents:UIControlEventTouchUpInside];
}


- (void)closeBtn {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)up {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)login {
    
    if (![BCBaseObject isMobileNumber:self.nameTextFiled.text]) {
        
        [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"手机号码不正确"];
        return;
    }else if (self.passwordTextField.text.length == 0) {
        
        [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"密码不能为空"];
        return;
    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"user_name"] = self.nameTextFiled.text;
    dic[@"password"] = self.passwordTextField.text;
    [[AppData shareData] loginWithInfo:dic withFinishBlock:^(NSDictionary *dic) {
        
        if ([dic[@"status"] integerValue] == 100) {
            
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"用户不存在"];
        }else if ([dic[@"status"] integerValue] == 2) {
            
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"密码错误"];
        }else if ([dic[@"status"] integerValue] == 1) {
            
            [userInfoTool updateInfo:dic[@"data"]];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }];
}


- (void)weiXin {
    
}

- (void)weiBo {
    
    
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response) {
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic[@"type"] = @"2";
        dic[@"open_id"] = response.data[@"sina"][@"usid"];
        dic[@"nickName"] = response.data[@"sina"][@"username"];
        dic[@"head"] = response.data[@"sina"][@"icon"];
        
    });
    
}

- (void)QQ {
    
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
    });
}

- (void)forget {
    
    FindPasswordViewController *findVC = [[FindPasswordViewController alloc] init];
    [self.navigationController pushViewController:findVC animated:YES];
}


@end
